﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// 
/// PayPal REST Payments API "payment object" modeled as classes for convenient 
/// JSON serialization / deserialization.
/// 
/// PayPal REST Payments API: 
///     https://developer.paypal.com/docs/api/payments/
/// 
/// Generate C# classes from JSON (3rd party helper): 
///     http://tools4geeks.com/json-to-Csharp-classes
///     
/// </summary>

public class PayPalObjects
{


    // classes used by root classes

    public class Details
    {
        public string shipping { get; set; }
        public string subtotal { get; set; }
        public string tax { get; set; }
        public string insurance { get; set; }
        public string handling_fee { get; set; }
        public string shipping_discount { get; set; }
    }

    public class Amount
    {
        public string currency { get; set; }
        public string total { get; set; }
        public Details details { get; set; }
    }

    public class Item
    {
        public string name { get; set; }
        public string quantity { get; set; }
        public string price { get; set; }
        public string sku { get; set; }
        public string currency { get; set; }
    }

    public class ItemList
    {
        public IList<Item> items { get; set; }
    }

    public class ShippingAddress
    {
        public string recipient_name { get; set; }
        public string line1 { get; set; }
        public string line2 { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        //public string phone { get; set; }
        public string postal_code { get; set; }
        public string country_code { get; set; }
    }

    public class ItemListWithShipping
    {
        public IList<Item> items { get; set; }
        public ShippingAddress shipping_address { get; set; }
    }

    public class Transaction
    {
        public Amount amount { get; set; }
        public string description { get; set; }
        public ItemList item_list { get; set; }
    }

    public class TransactionWithShipping
    {
        public Amount amount { get; set; }
        public string description { get; set; }
        public string custom { get; set; }
        public ItemListWithShipping item_list { get; set; }
    }

    public class Payer
    {
        public string payment_method { get; set; }
    }

    public class RedirectUrls
    {
        public string cancel_url { get; set; }
        public string return_url { get; set; }
    }



    // two root classes

    public class ExpressCheckoutPaymentData
    {
        public IList<Transaction> transactions { get; set; }
        public Payer payer { get; set; }
        public string intent { get; set; }
        public RedirectUrls redirect_urls { get; set; }

        public ExpressCheckoutPaymentData(string cancelUrl, string placeOrderUrl)
        {
            intent = "sale";

            payer = new PayPalObjects.Payer
            {
                payment_method = "paypal"
            };

            redirect_urls = new PayPalObjects.RedirectUrls
            {
                cancel_url = cancelUrl,
                return_url = placeOrderUrl
            };

            transactions = new List<PayPalObjects.Transaction>
            {
                new PayPalObjects.Transaction
                {
                    amount = new PayPalObjects.Amount
                    {
                        currency = "USD",
                        total = "320",
                        details = new PayPalObjects.Details
                        {
                            shipping = "2",
                            subtotal = "300",
                            tax = "5",
                            insurance = "10",
                            handling_fee = "5",
                            shipping_discount = "-2"
                        }
                    },
                    description = "creating a payment",
                    item_list = new PayPalObjects.ItemList
                    {
                        items = new List<PayPalObjects.Item>
                        {
                            new PayPalObjects.Item
                            {
                                name = "Camera",
                                quantity = "1",
                                price = "300",
                                sku = "1",
                                currency = "USD"
                            }
                        }
                    }
                }
            };
        }
    }

    public class ExpressCheckoutShippingPaymentData
    {
        public IList<TransactionWithShipping> transactions { get; set; }
        public Payer payer { get; set; }
        public string intent { get; set; }
        public RedirectUrls redirect_urls { get; set; }

        // constructor
        public ExpressCheckoutShippingPaymentData(string cancelUrl, string payUrl)
        {

            intent = "sale";

            payer = new PayPalObjects.Payer
            {
                payment_method = "paypal"
            };

            redirect_urls = new PayPalObjects.RedirectUrls
            {
                cancel_url = cancelUrl,
                return_url = payUrl
            };

            transactions = new List<PayPalObjects.TransactionWithShipping>
            {
                new PayPalObjects.TransactionWithShipping
                {
                    amount = new PayPalObjects.Amount
                    {
                        currency = "USD",
                        total = "320",
                        details = new PayPalObjects.Details
                        {
                            shipping = "2",
                            subtotal = "300",
                            tax = "5",
                            insurance = "10",
                            handling_fee = "5",
                            shipping_discount = "-2"
                        }
                    },
                    description = "Creating a payment",
                    custom = "",
                    item_list = new PayPalObjects.ItemListWithShipping
                    {
                        items = new List<PayPalObjects.Item>
                        {
                            new PayPalObjects.Item
                            {
                                name = "Camera",
                                quantity = "1",
                                price = "300",
                                sku = "1",
                                currency = "USD"
                            }
                        },
                        shipping_address = new PayPalObjects.ShippingAddress {
                            recipient_name = "",
                            line1 = "",
                            line2 = "",
                            city = "",
                            state = "",
                            postal_code = "",
                            country_code = ""
                        }
                    }
                }
            };
        }
    }


}